package com.network.mvp;

import com.network.api.UserApi;
import com.network.bean.User;
import com.network.proxy.MyRetrofit;
import com.network.proxy.adapter.RxCallAdapter;
import com.network.proxy.converter.GsonConverterFactory;

import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by kkmike999 on 2016/06/05.
 */
public class UserPresenter {

	UserApi  userApi;
	UserView userView;

	public UserPresenter(UserView userView) {
		this.userView = userView;

		userApi = new MyRetrofit.Builder().callAdapter(new RxCallAdapter())
		                                  .converter(new GsonConverterFactory())
		                                  .build()
		                                  .create(UserApi.class);
	}

	public UserPresenter(UserApi userApi, UserView userView) {
		this.userApi = userApi;
		this.userView = userView;
	}

	public void loadUser(String value) {
		//		User user = userApi.loadUser("test");
		//
		//		System.out.println(user);

		userApi.loadUserO(value)
		       .subscribeOn(Schedulers.io())
		       .observeOn(Schedulers.io())
		       .subscribe(new Action1<User>() {
			       @Override
			       public void call(User user) {
				       userView.onUserLoaded(user);
			       }
		       });
	}
}
